/*!
 @header InfoPickerViewController.m
 @author 周益
 @version 1.0.0 2013/02/22 Creation
 Copyright (c) 2013年 mypocketbaby. All rights reserved.
 */

#import "InfoPickerViewController.h"
#import "Helper.h"
#import "Config.h"
#import <QuartzCore/QuartzCore.h>
#import "RegexKitLite.h"

#define MAX_WORDS_NUMBER 70

//  -业务逻辑细节说明-
//
//  @abstract       InfoPickerViewController功能较为简单，根据枚举变量_userInfoType来判断VC具体要处理的业务，不同的业务功能（修改姓名和个性签名）显示不同的view。
//                  view结构简单，如果是修改姓名，那么view为一个textfield和提示语label；如果是修改个性签名，那么view为一个textview和提示语label。

//  @discussion
//
//

//  @notes          当为个性签名的功能时，就要跟踪记录用户输入的字数。


//  -类修改日志-
//  修改人   修改时间   针对版本
//  修改描述

@interface InfoPickerViewController ()

@end

@implementation InfoPickerViewController
@synthesize delegate = _delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

/*!
 @abstract InfoPickerViewController带参数的初始化方法
 @param content 用户预设置的内容
 @param type 此页面需要完成的功能
 @result InfoPickerViewController的实例
 */
- (id)initWithContent:(NSString *)content AndType:(UserInfoType)type
{
    self = [super init];
    if (self) {
        _content = content;
        _userInfoType = type;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    UIButton *backButton = [Helper buttonWithStyle:ButtonStyleNavigationReturn];
    [backButton addTarget:self action:@selector(backButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    UIButton *finishButton = [Helper buttonWithStyle:ButtonStyleNavigationDone];
    [finishButton addTarget:self action:@selector(finishButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:finishButton];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    
    self.view.backgroundColor = _SYS_COLOR_BACKGROUND;
    
    //输入提示
    UILabel *checkingLabel;
    
    if (_userInfoType == UserInfoNameType) {
        
        //textfield
        _nameTextField = [Helper textField:CGRectMake(10.0f, 10.0f, 300.0f, 44.0f) byPlaceholder:nil];
        _nameTextField.borderStyle = UITextBorderStyleRoundedRect;
        _nameTextField.text = _content;
        _nameTextField.delegate = self;
        _nameTextField.font = _SYS_FONT_007;
        [_nameTextField becomeFirstResponder];
        [self.view addSubview:_nameTextField];
        
        //输入提示
        checkingLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 64.0f, 300.0f, 44.0f)];
        checkingLabel.text = _L(@"holder_11");   //2-15个字，支持中英文、数字
        checkingLabel.font = _SYS_FONT_007;
        checkingLabel.textColor = _SYS_COLOR_002;
        checkingLabel.backgroundColor = [UIColor clearColor];
        
        [self.view addSubview:checkingLabel];
        
    }else if (_userInfoType == UserInfoSignatrueType){
        
        //textView
        _signatureTextView = [[UITextView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 300.0f, 120.0f)];
        _signatureTextView.tag = 100;
        _signatureTextView.text = _content;
        _signatureTextView.delegate = self;
//        _signatureTextView.backgroundColor = [UIColor whiteColor];
        [_signatureTextView becomeFirstResponder];
        
        _signatureTextView.font = _SYS_FONT_007;
        
        //实现textview的圆角框
//        _signatureTextView.layer.backgroundColor = [[UIColor clearColor] CGColor];
        _signatureTextView.layer.borderColor = _SYS_COLOR_002.CGColor;
        _signatureTextView.layer.borderWidth = 2.0;
        
        _signatureTextView.layer.cornerRadius = 8.0f;
        
        [_signatureTextView.layer setMasksToBounds:YES];
        
        [self.view addSubview:_signatureTextView];
        
        
        //字数统计
        _countLabel = [[UILabel alloc] initWithFrame:CGRectMake(275.0f, 110.0f, 30.0f, 20.0f)];
        _countLabel.text = [NSString stringWithFormat:@"%i", MAX_WORDS_NUMBER - _content.length];
        _countLabel.font = _SYS_FONT_007;
        _countLabel.textColor = _SYS_COLOR_002;
        _countLabel.backgroundColor = [UIColor clearColor];
        
        [self.view addSubview:_countLabel];
        
        //输入提示
        checkingLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, _signatureTextView.frame.origin.y + _signatureTextView.frame.size.height, 300.0f, 44.0f)];
        checkingLabel.text = _L(@"holder_12");   //请不要超过70个字
        checkingLabel.font = _SYS_FONT_007;
        checkingLabel.textColor = _SYS_COLOR_002;
        checkingLabel.backgroundColor = [UIColor clearColor];
        
        [self.view addSubview:checkingLabel];
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 自定义事件

/*!
 @abstract 返回按钮事件
 */
- (void)backButtonEvent:(id)sender
{
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

/*!
 @abstract 完成按钮事件
 */
- (void)finishButtonEvent:(id)sender
{
    if (_userInfoType == UserInfoNameType) {
        
        if ([_nameTextField.text length] > 15 || [_nameTextField.text length] < 2) {
            
            [Helper showAlertWithTitle:nil andMessage:_L(@"alert_32")];   //您的姓名不能低于2个字，不能超过15个字
            return;
        }
        if ([_delegate respondsToSelector:@selector(receivingName:)]) {
            
            [_delegate receivingName:_nameTextField.text];
        }
    }else if (_userInfoType == UserInfoSignatrueType){
        
        if ([_signatureTextView.text length] > 70) {
           
            [Helper showAlertWithTitle:nil andMessage:_L(@"alert_33")];   //您的个性签名不能超过70个字符
            return;
        }
        
        if ([_delegate respondsToSelector:@selector(receivingSignature:)]) {
            
            [_delegate receivingSignature:_signatureTextView.text];
        }
    }    
    
    [self.navigationController dismissModalViewControllerAnimated:YES];
    
}

#pragma mark TextVeiw Delegate

- (void)textViewDidChange:(UITextView *)textView {
    
	if (textView.tag == 100) {
        
        if ([textView.text length] > MAX_WORDS_NUMBER) {            
            
            textView.text = [textView.text substringToIndex:MAX_WORDS_NUMBER];
            
        }
        
        _countLabel.text = [NSString stringWithFormat:@"%i", MAX_WORDS_NUMBER - _signatureTextView.text.length];
        
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    return YES;
}

#pragma mark UITextField Delegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
    return YES;
}


- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if ([textField.text length] > 15) {
        
        textField.text = [textField.text substringToIndex:15];
        
        return NO;
    }
    return YES;
}
@end
